package com._520it.day2._05datasource.dbcp;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;

import com._520it.util.DBCPUtil;
import com._520it.util.DruidUtil;
import com._520it.util.JdbcUtil;

public class DBCPTest {
	
	public static DataSource getDataSource(){
		BasicDataSource ds=new BasicDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUsername("root");
		ds.setPassword("admin");
		ds.setUrl("jdbc:mysql:///jdbcdemo");
		ds.setMaxActive(5);//最大连接数
		return ds;
	}
	
	@Test
	public void testQuery() throws Exception {
//		Connection conn = getDataSource().getConnection();
//		Connection conn = DBCPUtil.getConn();
		Connection conn = DruidUtil.getConn();
		String sql="SELECT * FROM t_student";
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			long id = rs.getLong("id");
			String name = rs.getString("name");
			System.out.println(id+"\t"+name);
		}
		JdbcUtil.close(conn, ps, rs);
	}
}
